package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dao.ProductProcurementMapper;
import com.dao.SysUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.DeviceReplenishRecord;
import com.model.ProductProcurement;
import com.model.SysUser;
import com.page.GenericPage;
import com.po.ProductProcurementPO;
import com.po.ProductProcurementQueryPO;
import com.result.Result;
import com.service.ProductProcurementService;
import com.utils.CurrentLoginUserAgent;
import com.utils.NotNullBeanUtils;
import com.vo.DeviceReplenishRecordVO;
import com.vo.ProductProcurementVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.testng.collections.Lists;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: chihaojie
 * @Date: 2019/7/31 15:51
 * @Version 1.0
 * @Note
 */

@Slf4j
@Service
public class ProductProcurementServiceImpl  implements ProductProcurementService {

    @Autowired
    private ProductProcurementMapper   productProcurementMapper;

    @Autowired
    private SysUserMapper        sysUserMapper;

    @Override
    public Boolean save(ProductProcurementPO procurementPO) {
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        SysUser user =CurrentLoginUserAgent.getUser();
        Date now=new Date();
        ProductProcurement  procurement =new ProductProcurement();
        BeanUtils.copyProperties(procurementPO,procurement);
        procurement.setAgent(businessId);
        procurement.setProcurementTime(now);
        procurement.setSysUserId(user.getId());
        procurement.setSysUserName(user.getUserName());
        procurement.setCreated(user.getUserName());
        procurement.setCreateTime(now);
        procurement.setStatus(1);
        productProcurementMapper.insert(procurement);
        return true;
    }

    @Transactional
    @Override
    public Boolean edit(ProductProcurementPO procurementPO) {
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        SysUser user =CurrentLoginUserAgent.getUser();
        Date now=new Date();
        ProductProcurement productProcurement = productProcurementMapper.selectByPrimaryKey(procurementPO.getId());
        Optional.ofNullable(productProcurement).ifPresent(e->{
            NotNullBeanUtils.copyNotNullProperties(procurementPO,productProcurement);
            productProcurement.setUpdated(user.getUserName());
            productProcurement.setUpdateTime(now);
            productProcurementMapper.updateByPrimaryKey(productProcurement);
        });
        return true;
    }

    @Override
    public Boolean remove(Long id) {
        productProcurementMapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public ProductProcurementVO queryDetailInfo(Long id) {
        ProductProcurementVO vo =new ProductProcurementVO();
        ProductProcurement productProcurement = productProcurementMapper.selectByPrimaryKey(id);
        Optional.ofNullable(productProcurement).ifPresent(pp->{BeanUtils.copyProperties(productProcurement,vo);});
        return vo;
    }

    @Override
    public GenericPage<ProductProcurementVO> queryProductProcurementPage(Integer pageNo, Integer pageSize, ProductProcurementQueryPO queryPO) {
        Long businessId =CurrentLoginUserAgent.getBusinessId();
        SysUser user =CurrentLoginUserAgent.getUser();
        ProductProcurement condition=new ProductProcurement();
        if(user.getUserType().equals(1)){
        }else{
            condition.setAgent(businessId);
        }
        NotNullBeanUtils.copyNotNullProperties(queryPO,condition);
        final List<ProductProcurementVO> list= Lists.newArrayList();
        PageInfo<ProductProcurementVO> pageInfo = PageHelper.startPage(pageNo,pageSize).doSelectPageInfo(() -> {
            List<ProductProcurement> replenishRecordList = productProcurementMapper.selectByCondition(condition);
            Optional.ofNullable(replenishRecordList).ifPresent(e -> {
                List<ProductProcurementVO> collect = replenishRecordList.stream().map(r -> {
                    ProductProcurementVO vo = new ProductProcurementVO();
                    BeanUtils.copyProperties(r, vo);
                    SysUser agent = sysUserMapper.selectByPrimaryKey(vo.getAgent());
                    Optional.ofNullable(agent).ifPresent(a->{vo.setAgentName(agent.getUserName());});
                    list.add(vo);
                    return vo;
                }).collect(Collectors.toList());
            });
        });
        GenericPage<ProductProcurementVO> genericPage = new GenericPage<ProductProcurementVO>(pageSize, pageInfo.getTotal(), list);
        return genericPage;
    }
}
